package cn.t.keycloak.demo.mfa.sms.spi.impl;

import cn.t.keycloak.demo.mfa.sms.spi.ISmsSenderProvider;
import cn.t.keycloak.demo.mfa.sms.util.SmsCache;
import lombok.extern.slf4j.Slf4j;
import org.keycloak.models.KeycloakSession;

/**
 * @author 陶敏麒
 * @date 2023/11/20 15:41
 */
@Slf4j
public class SmsSenderProviderImpl implements ISmsSenderProvider {

    private final KeycloakSession session;

    private final String realmName;

    public SmsSenderProviderImpl(KeycloakSession session) {
        this.session = session;
        this.realmName = session.getContext().getRealm().getName();
    }

    /**
     * 发送短信
     * @return 过期时间,秒
     */
    @Override
    public int sendVerificationCode(String phoneNumber, String code) {
        // 这里调用具体实现类的SPI即可
        log.info("领域[{}]中发送手机[{}]验证码[{}]", this.realmName, phoneNumber, code);
        SmsCache.putCode(this.realmName, phoneNumber, code);
        return SmsCache.EXPIRE_SEC;
    }

    @Override
    public void close() {}
}
